/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Gui;

import Params.ServerSocketItem;
import java.io.File;
import java.io.FileInputStream;
import java.net.Socket;
import javax.swing.JFileChooser;

/**
 *
 * @author ANH PHONG
 */
public class frmSendFile extends javax.swing.JInternalFrame {

    public static String strFilePath = "";
    public static String strFileName = "";
    public byte[] arrByteOfSendFile;
    Socket _sockdangchon = null;

    /**
     * Creates new form frmSendFile
     */
    public frmSendFile() {
        initComponents();
    }

    frmSendFile(Socket temp) {
        initComponents();
        _sockdangchon = temp;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jtextpath = new javax.swing.JTextField();
        jbtnchonfile = new javax.swing.JButton();
        jbtnsend = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);

        jbtnchonfile.setText("Browse...");
        jbtnchonfile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnchonfileActionPerformed(evt);
            }
        });

        jbtnsend.setText("Send");
        jbtnsend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnsendActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jtextpath, javax.swing.GroupLayout.PREFERRED_SIZE, 284, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnchonfile)
                .addGap(0, 25, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(136, 136, 136)
                .addComponent(jbtnsend)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtextpath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnchonfile))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnsend)
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnchonfileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnchonfileActionPerformed
        // TODO add your handling code here:
        arrByteOfSendFile = null;
        JFileChooser choosen = new JFileChooser();
        choosen.showOpenDialog(null);
        File s = choosen.getSelectedFile();
        strFilePath = s.toString();
        int index = strFilePath.lastIndexOf("\\");
        strFileName = strFilePath.substring(index + 1);
        this.jtextpath.setText(strFilePath + strFileName);
    }//GEN-LAST:event_jbtnchonfileActionPerformed

    private void jbtnsendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnsendActionPerformed
        // TODO add your handling code here:
         try {
            //lay file du lieu
            FileInputStream inFromHardDisk = new FileInputStream(strFilePath);
            //lay kich thuoc file
            int size = inFromHardDisk.available();
            //khoi tao 1 mang byte(vi du lieu truyen di la byte)
            arrByteOfSendFile = new byte[size];
            //doc du lieu vao mang byte roi goi di
            inFromHardDisk.read(arrByteOfSendFile, 0, size);
            for (int i = 0; i < MainSever.listConnection.size(); i++) {
                ServerSocketItem tempServer = (ServerSocketItem) MainSever.listConnection.elementAt(i);
                if (_sockdangchon.getPort() + 2 == tempServer.getSOCKS().getPort()) {
                    tempServer.getConnectionSocketSendFile().outToClient.writeObject("IsFileTransfered");
                    tempServer.getConnectionSocketSendFile().outToClient.flush();
                    tempServer.getConnectionSocketSendFile().outToClient.writeObject(strFileName);
                    tempServer.getConnectionSocketSendFile().outToClient.flush();
                    tempServer.getConnectionSocketSendFile().outToClient.writeObject(arrByteOfSendFile);
                    tempServer.getConnectionSocketSendFile().outToClient.flush();
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
         this.dispose();

    }//GEN-LAST:event_jbtnsendActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jbtnchonfile;
    private javax.swing.JButton jbtnsend;
    private javax.swing.JTextField jtextpath;
    // End of variables declaration//GEN-END:variables
}
